<template>
  <div class="add-topic">
    <a-modal
      class="loading"
      :visible="visible"
      width="50%"
      title="评阅批量设置"
      @cancel="handleClose"
      @ok="handleOk"
    >
      <a-form class="form" :model="addSubjectForm" name="basic" ref="formRef" :rules="rules">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="试评试卷份数" name="spsjfs">
              <a-input-number
                :disabled="addSubjectForm.kmPyzt == 'FORMAL' || addSubjectForm.kmPyzt == 'TEST'"
                v-model:value="addSubjectForm.spsjfs"
                :min="0"
              />
            </a-form-item>
          </a-col>
          <a-col :span="16">
            <a-form-item label="质检比率（%）" name="zjbl">
              <a-input-number
                :disabled="addSubjectForm.kmPyzt == 'FORMAL' || addSubjectForm.kmPyzt == 'TEST'"
                v-model:value="addSubjectForm.zjbl"
                :min="0"
                :max="100"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="复评类型" name="fpCountType">
              <a-radio-group
                :disabled="addSubjectForm.kmPyzt == 'FORMAL'"
                v-model:value="addSubjectForm.fpCountType"
              >
                <a-radio-button value="RATIO">按比例</a-radio-button>
                <a-radio-button value="COUNT">按份数</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="8" v-if="addSubjectForm.fpCountType == 'RATIO'">
            <a-form-item
              label="复评比率（%）"
              :disabled="addSubjectForm.kmPyzt == 'FORMAL'"
              name="fpbl"
            >
              <a-input-number v-model:value="addSubjectForm.fpbl" :min="0" :max="100" />
            </a-form-item>
          </a-col>
          <a-col :span="8" v-if="addSubjectForm.fpCountType == 'COUNT'">
            <a-form-item
              label="复评份数"
              :disabled="addSubjectForm.kmPyzt == 'FORMAL'"
              name="fpCount"
            >
              <a-input-number v-model:value="addSubjectForm.fpCount" :min="0" /> 份
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, defineEmits, onMounted, defineProps, computed } from 'vue';
  import { message } from 'ant-design-vue';
  import { saveOrUpdateAllSubject } from '/@/api/organization/subjectManage';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();
  const userInfo = <any>computed(() => userStore.getUserInfo);
  const props = defineProps({
    visible: {
      type: Boolean,
    },
  });

  const addSubjectForm: any = ref({});

  const rules = {
    spsjfs: [
      {
        required: true,
        validator: (rule, value, callback) => {
          if (value <= 0) {
            return Promise.reject('试评试卷份数需要大于0');
          } else {
            return Promise.resolve();
          }
        },
        trigger: 'change',
      },
      { required: true, message: '试评试卷份数不能为空', trigger: 'blur' },
    ],
    ydsjFs: [
    //   {
    //     required: true,
    //     validator: (rule, value, callback) => {
    //       if (value <= 0) {
    //         return Promise.reject('应到份数需要大于0');
    //       } else {
    //         return Promise.resolve();
    //       }
    //     },
    //     trigger: 'change',
    //   },
      { required: true, message: '应到份数不能为空', trigger: 'blur' },
    ],
    zjbl: [
      { required: true, message: '质检比率不能为空', trigger: 'blur' },
    //   {
    //     required: true,
    //     validator: (rule, value, callback) => {
    //       if (value <= 0) {
    //         return Promise.reject('质检比率需要大于0');
    //       } else {
    //         return Promise.resolve();
    //       }
    //     },
    //     trigger: 'change',
    //   },
    ],
    fpbl: [
    //   {
    //     required: true,
    //     validator: (rule, value, callback) => {
    //       if (value <= 0) {
    //         return Promise.reject('复评比率需要大于0');
    //       } else {
    //         return Promise.resolve();
    //       }
    //     },
    //     trigger: 'change',
    //   },
      { required: true, message: '复评比率不能为空', trigger: 'blur' },
    ],
    fpCount: [
    //   {
    //     required: true,
    //     validator: (rule, value, callback) => {
    //       if (value <= 0) {
    //         return Promise.reject('复评份数需要大于0');
    //       } else {
    //         return Promise.resolve();
    //       }
    //     },
    //     trigger: 'change',
    //   },
      { required: true, message: '复评份数不能为空', trigger: 'blur' },
    ],
    fpCountType: [{ required: true, message: '复评类型不能为空', trigger: 'blur' }],
  };

  const emits = defineEmits(['cancel', 'refresh']);

  const formRef = ref();
  const handleClose = async () => {
    emits('cancel');
    await formRef.value.resetFields();
  };
  const handleOk = async () => {
    try {
      await formRef.value.validate();
      addSubjectForm.value.kmYear = userInfo.value.kmYear;
      saveOrUpdateAllSubject(addSubjectForm.value).then((res) => {
        if (res.code == 200) {
          message.success(`设置成功`);
          emits('refresh');
          handleClose();
        } else {
          message.error(res.message);
        }
      });
    } catch (error) {
      console.log(error);
    }
  };

  onMounted(() => {});
</script>
<style lang="less" scoped>
  .form {
    width: 100%;
    padding: 16px;
  }
</style>
